﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using AForge.Video;
using AForge.Video.DirectShow;

namespace TwoGesture
{
    public partial class Formulario_usuario : Form
    {
        private FilterInfoCollection videodivices = null;
        private VideoCaptureDevice videosources = null;
        private Size framesize;
        private int framerate;
        public Bitmap currentimagen;

        public Formulario_usuario()
        {
            InitializeComponent();
            this.framesize = new Size(320, 240);
            this.framerate = 30;
            this.currentimagen = null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.star();
            this.btnFin.Visible = true;
            this.btninicio.Visible = false;
        }

        private FilterInfoCollection getcamlist()
        {
            videodivices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            return videodivices;
        }

        public void star()
        {
            if (getcamlist().Count == 0)
                throw new
                Exception("No hay camara");
            else
            {
                videosources = new VideoCaptureDevice(videodivices[0].MonikerString);

                videosources.DesiredFrameSize = this.framesize;
                videosources.DesiredFrameRate = this.framerate;
                videosources.Start();
                videosources.NewFrame += new NewFrameEventHandler(video_newFrame);
            }

        }

        private bool imagenconvertcallback()
        {
            return false;
        }

        private void video_newFrame(object sender, NewFrameEventArgs eventArgs)
        {
            this.pboxcam.Image = eventArgs.Frame.GetThumbnailImage(framesize.Width, framesize.Height, new Image.GetThumbnailImageAbort(imagenconvertcallback), IntPtr.Zero);
        }

        public void stop()
        {
            if (!(videosources == null))
                if (videosources.IsRunning)
                {
                    videosources.SignalToStop();
                    videosources = null;
                }
            String path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

            string archivo = path + "\\LogoTwoGestures.gif";

            this.pboxcam.ImageLocation = "\\LogoTwoGestures.gif";
        }

        private void cerrarSesionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Login lg = new Login();
            lg.Show();
            this.Hide();
            
            
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void btnFin_Click(object sender, EventArgs e)
        {
            this.stop();
            this.btnFin.Visible = false;
            this.btninicio.Visible = true;
        }
    }
}
